package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.Material;
import cn.stylefeng.guns.modular.business.mapper.MaterialMapper;
import cn.stylefeng.guns.modular.business.model.params.MaterialParam;
import cn.stylefeng.guns.modular.business.model.result.MaterialResult;
import  cn.stylefeng.guns.modular.business.service.MaterialService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 推广素材 服务实现类
 * </p>
 *
 * @author pzp
 * @since 2019-07-30
 */
@Slf4j
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Value("${server.port}")
    private String port;

    @Override
    public void add(MaterialParam param){
        Material entity = getEntity(param);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDeleteFlag(RecoverConst.DEL_FLAG_FALSE);
        this.save(entity);
    }

    @Override
    public void delete(MaterialParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(MaterialParam param){
        Material oldEntity = getOldEntity(param);
        Material newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateTime(new Date());
        this.updateById(newEntity);
    }

    @Override
    public MaterialResult findBySpec(MaterialParam param){
        return null;
    }

    @Override
    public List<MaterialResult> findListBySpec(MaterialParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(String type, String beginTime, String endTime){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, type,beginTime,endTime);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MaterialParam param){
        return param.getMaterialId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Material getOldEntity(MaterialParam param) {
        return this.getById(getKey(param));
    }

    private Material getEntity(MaterialParam param) {
        Material entity = new Material();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 获取素材列表
     *
     * @param type    素材类型：0：问安，1：图文，2：素材：3：晒单
     * @param current 第几页
     * @param size    每页条数
     * @return
     */
    @Override
    public Page<MaterialResult> get(String type, Integer current, Integer size) {
        Page<MaterialResult> page=baseMapper.customPageList(new Page<>(current,size), type,"","");
        return page;
    }
}
